package com.ruoyi.finance.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.finance.mapper.PaymentSlipsMapper;
import com.ruoyi.finance.domain.PaymentSlips;
import com.ruoyi.finance.service.IPaymentSlipsService;
import org.springframework.util.NumberUtils;
/**
 * 付款单Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-29
 */
@Service
public class PaymentSlipsServiceImpl implements IPaymentSlipsService 
{
    @Autowired
    private PaymentSlipsMapper paymentSlipsMapper;

    /**
     * 查询付款单
     * 
     * @param paymentId 付款单主键
     * @return 付款单
     */
    @Override
    public PaymentSlips selectPaymentSlipsByPaymentId(Long paymentId)
    {
        return paymentSlipsMapper.selectPaymentSlipsByPaymentId(paymentId);
    }

    /**
     * 查询付款单列表
     * 
     * @param paymentSlips 付款单
     * @return 付款单
     */
    @Override
    public List<PaymentSlips> selectPaymentSlipsList(PaymentSlips paymentSlips)
    {
        return paymentSlipsMapper.selectPaymentSlipsList(paymentSlips);
    }

    /**
     * 新增付款单
     * 
     * @param paymentSlips 付款单
     * @return 结果
     */
    @Override
    public int insertPaymentSlips(PaymentSlips paymentSlips)
    {
        paymentSlips.setCreateTime(DateUtils.getNowDate());
        return paymentSlipsMapper.insertPaymentSlips(paymentSlips);
    }

    /**
     * 修改付款单
     * 
     * @param paymentSlips 付款单
     * @return 结果
     */
    @Override
    public int updatePaymentSlips(PaymentSlips paymentSlips)
    {
        paymentSlips.setUpdateTime(DateUtils.getNowDate());
        return paymentSlipsMapper.updatePaymentSlips(paymentSlips);
    }

    /**
     * 批量删除付款单
     * 
     * @param paymentIds 需要删除的付款单主键
     * @return 结果
     */
    @Override
    public int deletePaymentSlipsByPaymentIds(Long[] paymentIds)
    {
        return paymentSlipsMapper.deletePaymentSlipsByPaymentIds(paymentIds);
    }

    /**
     * 删除付款单信息
     * 
     * @param paymentId 付款单主键
     * @return 结果
     */
    @Override
    public int deletePaymentSlipsByPaymentId(Long paymentId)
    {
        return paymentSlipsMapper.deletePaymentSlipsByPaymentId(paymentId);
    }

    /**
     * 批量添加付款单
     * @param paymentSlipsList list集合数据
     * @return 结果
     */
    @Override
    public int insertPaymentSlipsList(List<PaymentSlips> paymentSlipsList) {
        AtomicInteger index = new AtomicInteger(0);
        paymentSlipsList.stream().map(paymentSlips -> {
            paymentSlips.setCreateTime(DateUtils.getNowDate());
            paymentSlips.setPaymentOrder(generateCode(index.getAndIncrement()));
            return paymentSlips;
        })
        .collect(Collectors.toList());
        return paymentSlipsMapper.insertPaymentSlipsList(paymentSlipsList);
    }

    @Override
    public int updatePaymentApplicant(Long[] paymentIds,boolean flay, String paymentApplicant) {
        if (flay){
            return paymentSlipsMapper.updatePaymentApplicant(paymentIds, Constants.SUCCESS,paymentApplicant);
        }else{
            return paymentSlipsMapper.updatePaymentApplicant(paymentIds,Constants.FAIL,paymentApplicant);
        }
    }

    @Override
    public List<PaymentSlips> selectPaymentApplicantByPaymentId(Long[] paymentIds) {
        return paymentSlipsMapper.selectPaymentApplicantByPaymentId(paymentIds);
    }

    @Override
    public int updatePostingApplicant(Long[] paymentIds, String postingApplicant) {
        return paymentSlipsMapper.updatePostingApplicant(paymentIds,postingApplicant);
    }

    /**
     * 生成随机数
     * @return
     */
    private String generateCode(int size) {
        // 获取当前日期
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String currentDate = dateFormat.format(new Date());

        // 获取序列号
        String paymentOrder = paymentSlipsMapper.selectPaymentOrder();
        String randomNumber;
        // ...根据自己的业务逻辑实现获取序列号的方法
        if(StringUtils.isEmpty(paymentOrder)){
            randomNumber = Constants.RANDOM_NUMBERS;
        }else{
            randomNumber = String.format("%04d", (NumberUtils.parseNumber(paymentOrder.substring(10),Long.class)+(size+1)));
        }

        // 拼接生成的代码
        String code = Constants.CODE_SLIPS + currentDate + randomNumber;

        return code;
    }
}
